﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;

namespace Rapattern.Utilities
{
    public class KeyEventHandle
    {
        /// <summary>
        /// 勾子管理类 
        /// </summary>
        private KeyboardHookLib _keyboardHook = null;

        private readonly int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标移动
        private readonly int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下
        private readonly int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标左键抬起
        private readonly int MOUSEEVENTF_ABSOLUTE = 0x8000;//鼠标绝对位置
        private readonly int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 
        private readonly int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 
        private readonly int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下 
        private readonly int MOUSEEVENTF_MIDDLEUP = 0x0040;// 模拟鼠标中键抬起 

        private static KeyEventHandle instance;

        private KeyEventHandle() { }

        public static KeyEventHandle GetInstance()
        {
            if (instance == null)
            {
                instance = new KeyEventHandle();
            }
            return instance;
        }

        /// <summary>
        /// 安装勾子 
        /// </summary>
        public void InitializeHook()
        {
            try
            {
                _keyboardHook = new KeyboardHookLib();
                _keyboardHook.InstallHook(OnKeyPress);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary> 
        /// 客户端键盘捕捉事件. 
        /// </summary> 
        /// <param name="hookStruct">由Hook程序发送的按键信息</param> 
        /// <param name="handle">是否拦截</param> 
        public void OnKeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)
        {
            try
            {
                handle = true; //预设拦截键盘消息
                //handle = false; //预设不拦截键盘消息
            }
            catch (Exception ex)
            {
                handle = true;
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// 取消钩子
        /// </summary>
        public void UninstallHook()
        {
            try
            {
                if (_keyboardHook != null)
                {
                    _keyboardHook.UninstallHook();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// 键盘事件过滤
        /// </summary>
        /// <param name="vkCode"></param>
        /// <returns></returns>
        private bool FilterKey(int vkCode)
        {
            if (listKey.Contains((Keys)vkCode))
            {
                return true;
            }
            return false;
        }

        private static List<Keys> listKey = new List<Keys>() {
            Keys.Escape,
            Keys.Return,
            Keys.Enter,
            Keys.Apps,
            Keys.BrowserHome,
            Keys.Left,
            Keys.Right,
            Keys.Up,
            Keys.Down,
            Keys.VolumeDown,
            Keys.VolumeUp,
            Keys.MediaPlayPause
        };
    }
}
